package app.stock.tools;

import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import app.common.DatabaseManager;
import app.common.GlobalConstants;
import app.common.PropertyUtil;
import app.stock.common.Account;
import app.stock.common.Transaction;
import app.stock.common.TransactionTypes;
import app.stock.server.StockManager;

public class ExpFileProcessor {
	static String fileName = "C:\\Workspace\\training\\project\\testjava\\input\\20130208.xls";
	// static final String txtSpliter = "        ";
	static final String xlsSpliter = "=";
	static final DateFormat df = new SimpleDateFormat("yyyyMMdd HH:mm:ss");
	static StockManager sm = new StockManager();

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		PropertyUtil.put(GlobalConstants.key_appMode, GlobalConstants.value_appMode_desktop);
		GlobalConstants.listCurrent();
		DatabaseManager.init();
		parseFile();
		DatabaseManager.shutdownDB();
	}

	static void parseFile() {
		List<Transaction> txList = new ArrayList<Transaction>();
		try {
			LineNumberReader lnr = new LineNumberReader(new InputStreamReader(new FileInputStream(
					fileName), "GBK"));
			int lineNumber = 0;
			while (lnr.read() != -1) {
				String str = lnr.readLine();
				System.out.println(str);
				if (lineNumber >= 1) {
					Transaction tx = lineToTransaction(str);
					if (tx != null) {
						StockCalculator.caluclate(tx);
						sm.addTransaction(tx);
						txList.add(tx);
					}
				}
				lineNumber++;
			}
			lnr.close();
			System.out.println(txList);
			System.out.println("Import transaction done sucessfully");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	// 0发生日期 1成交时间 2业务名称 3证券代码 4证券名称 5成交价格 6成交数量 7成交金额 8股份余额 9手续费
	// 10印花税 11过户费 12交易所清算费 13发生金额 14资金本次余额 15委托编号 16股东代码 17资金帐号 18币种
	static Transaction lineToTransaction(String line) throws ParseException {
		Transaction tx = new Transaction();
		line = line.replaceAll("\"", "");
		String[] cols = line.split(xlsSpliter);
		Account acct = new Account();
		acct.name = "fucheng";
		tx.account = acct;
		try {
			tx.date = df.parse(cols[0].trim() + " " + cols[1].trim());
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw e;
		}
		tx.type = cols[2].trim();
		tx.code = cols[3].trim();
		tx.name = cols[4].trim();
		if (tx.type.equals(TransactionTypes.mairu) || tx.type.equals(TransactionTypes.maichu)) {
			tx.price = Float.parseFloat(cols[5].trim());
			tx.amount = (int) Float.parseFloat(cols[6].trim());
			tx.shouxufei = Float.parseFloat(cols[9].trim());
			tx.yinhuashui = Float.parseFloat(cols[10].trim());
			tx.guohufei = Float.parseFloat(cols[11].trim());
		} else if (tx.type.equals("银行转存")) {
			tx.sum1 = Float.parseFloat(cols[13].trim());
			tx.type = TransactionTypes.zhuanru;
		} else if (tx.type.equals("银行转取")) {
			tx.sum1 = Float.parseFloat(cols[13].trim());
			tx.type = TransactionTypes.zhuanchu;
		} else if (tx.type.equals("股息入帐")) {
			tx.sum1 = Float.parseFloat(cols[13].trim());
			tx.type = TransactionTypes.honggupaixi;
		} else if (tx.type.equals("红股入帐")) {
			tx.sum1 = Float.parseFloat(cols[13].trim());
			tx.amount = (int) Float.parseFloat(cols[6].trim());
			tx.type = TransactionTypes.honggupaixi;
		} else if (tx.type.equals("利息归本")) {
			tx.sum1 = Float.parseFloat(cols[13].trim());
			tx.type = TransactionTypes.lixi;
		} else {
			System.out.println("###################################" + tx.type);
		}
		if (tx.code == null) {
			tx.code = "";
		}
		if (tx.name == null) {
			tx.name = "";
		}
		return tx;
	}
}
